package com.ruitu.mall_98_seller.ui.msg;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruitu.arad.Arad;
import com.ruitu.arad.api.PageModel;
import com.ruitu.arad.base.base_list.BaseRefreshActivity;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.support.widget.progress.ProgressLayout;
import com.ruitu.arad.util.AnimationUtil;
import com.ruitu.arad.util.TimeUtils;
import com.ruitu.arad.util.UIUtils;
import com.ruitu.mall_98_seller.R;
import com.ruitu.mall_98_seller.dao.bean.Msg;
import com.ruitu.mall_98_seller.mvp.model.MsgModel;
import com.ruitu.mall_98_seller.mvp.presenter.MsgPresenter;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.util.RcvUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class MsgListActivity extends BaseRefreshActivity<MsgPresenter.MsgListP, MsgModel.MsgListM> {
    private TextView tv_empty;//空白页面

    private List<Msg> msgList = new ArrayList<>();// 消息列表
    private MsgListAdapter adapter;

    private int currPage = 1;
    private int totalPage = 1;
    private int currPosition;//当前点击的位置

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_msg_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tv_empty = findViewById(R.id.tv_empty);

        setHeadTitle("系统消息");
        setRightText("全标已读");

        initQQPop();
        RcvUtils.refreshCompleteDelay(lRecyclerView, null);
        Arad.bus.register(this);

        reqDataList();
    }

    @Override
    protected void onRightTextClick() {
        UIUtils.showAlertDialog(getSupportFragmentManager(), "温馨提示"
                , "是否将全部消息标记为已读？",
                "确定", "取消"
                , new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        hideProgress();
                        showProgressWithText(true, "操作中");
                        p.reqAllReadMsg(AppHolder.instance().getUser().getCompany().getId());
                    }
                }, null);
    }

    private void reqDataList() {
        p.reqMsgList(AppHolder.instance().getUser().getCompany().getId(), 20, currPage);
        showProgress();
    }

    @Override
    protected void onItemClick(View v, int position) {
        Bundle bundle = new Bundle();
        bundle.putString("msg_id", msgList.get(position).getId());
        startActivity(MsgDetailActivity.class, bundle);
        currPosition = position;
    }

    @Override
    protected void reqFirst() {
        msgList.clear();
        adapter.setDataList(msgList);
        currPage = 1;
        reqDataList();
    }

    @Override
    protected void reqMore() {
        currPage++;
        if (currPage > totalPage) {
            refreshNoMoreData();
        } else {
            reqDataList();
        }
    }

    @Override
    public void onReqComplete(int code, boolean isOk, Object data) {
        if (code == 1) {// 消息列表
            PageModel<Msg> pageMsg = (PageModel<Msg>) data;
            currPage = pageMsg.getCurrentPage();
            totalPage = pageMsg.getTotalPage();
            List<Msg> newData = pageMsg.getDataList();
            msgList.addAll(newData);
            adapter.setDataList(msgList);

            if (isDataEmpty(msgList)) {
                setEmptyViewVisible(View.VISIBLE);
            } else {
                setEmptyViewVisible(View.GONE);
            }
        }
        if (code == 2) {// 全部已读
            lRecyclerView.forceToRefresh();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetEvent(EventModel e) {
        if (e.getEventCode() == 18122851) {//消息已读
            msgList.get(currPosition).setMsgStatus("1");//0 未读  1 已读
            adapter.notifyItemChanged(currPosition);
        }
    }

    private TextView tv_all_read;//全部已读
    private TextView tv_all_del;//全部清空
    private View window;

    private void initQQPop() {
        window = findViewById(R.id.window);
        tv_all_read = window.findViewById(R.id.tv_all_read);
        tv_all_del = window.findViewById(R.id.tv_all_del);
        tv_all_read.setOnClickListener(this);
        tv_all_del.setOnClickListener(this);
        window.setOnClickListener(this);
    }

    // 设置是否显示空白view
    public void setEmptyViewVisible(int visible) {
        tv_empty.setVisibility(visible);
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return null;
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new MsgListAdapter(this);
        return adapter;
    }

    @Override
    protected LRecyclerView findRecyclerView() {
        return findViewById(R.id.rcv_msg);
    }

    private class MsgListAdapter extends ListBaseAdapter<Msg> {

        public MsgListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_msg_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            View flag_unread = holder.getView(R.id.flag_unread);
            TextView tv_title = holder.getView(R.id.tv_title);
            TextView tv_time = holder.getView(R.id.tv_time);
            TextView tv_content = holder.getView(R.id.tv_content);
            Msg msg = msgList.get(position);
            if (TextUtils.equals(msg.getMsgStatus(), "1")) {//0 未读 1 已读
                flag_unread.setVisibility(View.INVISIBLE);
            } else {
                flag_unread.setVisibility(View.VISIBLE);
            }
            tv_title.setText(msg.getTitle());
            tv_content.setText(msg.getContent());
            try {
                String time = TimeUtils.getFriendlyTimeSpanByNow(Long.parseLong(msg.getCreateTime()));
//                tv_time.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm")
//                        .format(Long.parseLong(msg.getCreateTime())));
                tv_time.setText(time);
            } catch (Exception e) {
                e.printStackTrace();
                tv_time.setText("1970-01-01 08:00:00");
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (Arad.bus.isRegistered(this)) {
            Arad.bus.unregister(this);
        }
    }

    private void showOrHideWindow(boolean isShow) {
        if (isShow) {
            AnimationUtil.startAlphaAnima(window, 0, 1);
            window.setVisibility(View.VISIBLE);
        } else {
            AnimationUtil.startAlphaAnima(window, 1, 0);
            window.setVisibility(View.GONE);
        }
    }
}
